package com.framework.Controller;


import com.framerock.baselib.AA;
import com.framerock.db.DBWhere;
import com.framerock.server.Response;
import com.framerock.baselib.CDate;
import com.framerock.baselib.Jiami;
import com.framerock.baselib.Json;
import com.framerock.baselib.Patter;
import com.framerock.baselib.Rock;
import com.framerock.baselib.RockMsg;
import com.framerock.baselib.Xinhu;
import com.framework.ACore.Controller;
import com.framework.Chajian.ChajianSms;
import com.framework.ACore.Model;
import com.framework.Model.ModelLogin;
import com.framework.Model.ModelOption;

import java.util.Map;

public class ControllerLogin extends Controller {

    @Override
    protected String runAction(String action) {
        if(action.equals("find"))return this.find();
        if(action.equals("findcode"))return this.findcode();
        return super.runAction(action);
    }

    /**
     * 登录验证Post
     * */
    public String check()
    {
        return new ModelLogin(this.nDb).start(this.mRequest);
    }

    /**
     * 初始信息
     * */
    public String data()
    {
        Json json = new Json();
        json.putVal("systemtitle", Xinhu.getVal(this.nDb, "systemtitle"));
        json.putVal("findcode", Xinhu.getVal(this.nDb, "findcode"));
        json.putVal("version", AA.VERSION);
        return Response.returnSuccess(json.getJson());
    }

    /**
     * 找回密码
     * */
    public String find()
    {
        String name     = mRequest.post("name");
        String yzm      = mRequest.post("yzm");
        String mobile   = mRequest.post("mobile");
        String pass     = mRequest.post("pass");
        if(Rock.isEmpt(name) || Rock.isEmpt(mobile) || Rock.isEmpt(pass))return Response.returnError("null");
        String findcode = ModelOption.get(this.nDb).getVal("findcode");

        String where    = "`mobile`='"+mobile+"' AND `status`=1";
        if(findcode.equals("1")){
            if(Rock.isEmpt(yzm))return Response.returnError("yzm is empty");
        }else{
            where += " AND `name`='"+name+"'";
        }

        Model obj = Model.get(this.nDb, "users");
        Map<String,String> urs = obj.getOne(where);
        if(urs == null)return Response.returnError("此用户不存在");

        if(findcode.equals("1")){
            RockMsg rockmsg = ChajianSms.obj(this.nDb).CheckCode(mobile, urs.get("user"), yzm);
            if(rockmsg.isError())return rockmsg.back();
        }

        String rand = CDate.gettime();
        obj.update(Rock.getMap(new String[]{
                "rand",rand,
                "pass", Jiami.md5(pass+rand)
        }),urs.get("id"));

        return Response.returnSuccess();
    }

    private String findcode()
    {
        String mobile   = mRequest.get("mobile");
        if(Rock.isEmpt(mobile) || !Patter.isnumber(mobile))return Response.returnError("errcan");

        Model obj = Model.get(this.nDb, "users");
        Map<String,String> urs = obj.getOne(DBWhere.start().wstro("mobile", mobile).wint("status", 1).show());
        if(urs==null)return Response.returnError("手机号未注册");


        return ChajianSms.obj(this.nDb).sendCode(mobile, urs.get("user"));
    }
}
